<template>

  <el-dialog :title="id ? '支付页修改' : '支付页添加'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="30%">

    <el-form ref="main-form" :model="form" :rules="rules" label-width="120px" >
      <el-form-item label="支付页名称" size="mini" prop="pageName">
        <el-input placeholder="" v-model="form.pageName" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
      <el-form-item label="支付页链接" size="mini" prop="payPageUrl">
        <el-input placeholder="" v-model="form.payPageUrl" @keyup.enter.native="submit" clearable></el-input>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <!-- <el-popover
        v-if="id != null"
        placement="top"
        width="160"
        v-model="popDeleteVisible">
        <p>确定删除吗？</p>
        <div style="text-align: right; margin: 0">
          <el-button size="mini" type="default" @click="popDeleteVisible = false">取消</el-button>
          <el-button type="danger" size="mini" @click="submitDel">确定</el-button>
        </div>
        <el-button type="danger" slot="reference"  size="small" style="float: left;" :loading="loading">删 除</el-button>
      </el-popover> -->

      <el-button @click="close" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small" :loading="loading">提 交</el-button>
    </div>

  </el-dialog>
</template>

<script>

import ApiMain from '@/api/bank.payPage';

export default {
  mixins: [
  ],
  props: [
    'banTypeOptions',
  ],
  data () {
    return {

      dialogVisible: false,
      popDeleteVisible: false,
      loading: false,

      form: {
        banType: '',
        startValue : '',
        endValue : '',
      },

      rules: {
        pageName: [
          { required: true, message: '请输入名称', trigger: 'blur' },
        ],
        payPageUrl: [
          { required: true, message: '请输入链接', trigger: 'blur' },
        ],
      },

      id  : null,
      data: null,

    }
  },
  mounted () {
  },

  methods: {

    show (id = null) {

      this.id = id;

      if (this.id) {
        this.fetchData();
      }
      else {
        this.reset();
      }

      this.dialogVisible = true;
    },

    close () {
      this.dialogVisible = false;
      this.$emit('dialog-close');
    },

    reset () {
      this.form = {
        pageName: '',
        payPageUrl: '',
      };
    },

    fetchData () {

      ApiMain.info({id: this.id})
      .then(re => {
        this.data = re; 
      })
      .catch(() => {
        this.data = null;
      })
      .finally(() => {
        this.renderData();
      });
    },

    renderData () {
      this.form.pageName = this.data.pageName;
      this.form.payPageUrl = this.data.payPageUrl;
    },

    verifyForm () {
      var bool = true;
      this.$refs['main-form'].validate((valid) => bool = valid);
      return bool;
    },

    async submit () {

      if (! this.verifyForm()) return false;

      this.loading = true;

      try {
        if (this.id) {
          await this.submitSet();
        }
        else {
          await this.submitAdd();
        }
      } finally {
        this.loading = false;
      }
    },

    async submitDel() {

      this.popDeleteVisible = false;
      this.loading = true;

      await ApiMain.del({id: this.id});

      this.loading = false;
      this.close();
      this.$message({message: '删除成功', type: 'success'});
    },

    async submitSet () {

      this.form.id = this.id;
      await ApiMain.set(this.form);
      this.$message({message: '修改成功', type: 'success'});
      this.close();

    },

    async submitAdd () {

      await ApiMain.add(this.form);
      this.$message({message: '添加成功', type: 'success'});
      this.close();
    },
  }
}
</script>

<style>

</style>
